// pages/order/appraise/index.js
const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    textarea: '',
    isTextares: false,
    img: [],
    allAppra: 5,
    appra: 5,
    appra1: 5,
    order: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 读取菜品信息
    this.setData({
      order: wx.getStorageSync('canteenInfo')
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  countApprFun(e) {
    console.log(e);
    let str = e.currentTarget.dataset.id;
    let val = e.currentTarget.dataset.val;
    let date = {};
    date[str] = val;
    this.setData(date);
    let all = (this.data.appra + this.data.appra1) / 2;
    this.setData({
      allAppra: all
    })
  },
  upTextarea(e) {
    console.log(e)
    let val = e.detail.value;
    this.setData({
      textarea: val
    })
  },
    //获取七牛token
    uptoken() {
      return new Promise((r, v) => {
        App.ajax('open/upload/token', 'GET', {}).then(res => {
          if (res && res.data) {
            r(res.data)
          }
        }).catch(err => {
          console.log('报错了', err)
          v()
        })
      })
    },
  
    //七牛上传
    handleUploadImage: function () {
      if (this.data.img.length > 4) {
        App.prompts('最多三张');
        return false; 
      }
      const qiniuUploader = require("../../../utils/qiniuUploader-min");
      let that = this;
      let img = JSON.parse(JSON.stringify(this.data.img));
      wx.chooseImage({
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          let tempFilePaths = res.tempFilePaths; 
          tempFilePaths.map((r, i) => {
            that.uptoken().then(function (res) { //异步变同步
              let uptoken = res.token
              let domain = res.domain
              console.log(res, uptoken)
              qiniuUploader.upload(
                r, //上传的图片
                (res) => {  
                  console.log(res)
                  img.push(res.imageURL);
                  that.setData({
                    img: img
                  })
                  
                }, (error) => { 
                  console.log('error: ' + error);
                },
                { //华南地区
                  region: 'SCN',
                  uptoken: uptoken,
                  uploadURL: 'https://up-z2.qbox.me',
                  domain: domain+'/',
                })
            })
          })
        }
      })
    },
    appraiseFun() {
      let url = 'mp/order/evaluate';
      let param = {
        score: this.data.allAppra,
        order_dishes_id: this.data.order.order_id,
        content: this.data.textarea,
        imgs: this.data.img,
        dishes_list_id: this.data.order.canteen_id
      };
      App.ajax(url, 'POST', param).then(res => {
        if (res.status === 10000) {
          App.prompts(res.msg);
          wx.navigateBack({
            delta: 1
          })
        } else {
          App.prompts(res.msg);
        }
      }).catch(err => {
        console.log(err)
      })
    }
})